﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
//using Microsoft.Office.Interop.Owc11;
using System.IO;

public partial class appleader_result_viewResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int projectid = int.Parse(Request.QueryString[0].ToString());
            //判断任务是否全部完成，没有则返回
            if (Request.QueryString[0] != null)
            {
                if (!MasApp.BLL.Search.MissionAllFinish(projectid))
                {
                    Label lb = new Label();
                    lb.Text = "尚有项目任务未完成1，不能生成结果！";
                    lb.ForeColor = System.Drawing.Color.Red;
                    lb.Font.Size = FontUnit.Larger;
                    form1.Controls.AddAt(1, lb);
                    HyperLink hy = new HyperLink();
                    hy.NavigateUrl = "list.aspx";
                    hy.Text = "<br />返回";
                    form1.Controls.Add(hy);
                }

                //直接赋权是另外判断是否完成评权任务
                if (MasApp.BLL.Search.SearchProjectWeightType(projectid).Equals("直接赋权"))
                {
                    DataTable dt = MasApp.BLL.Search.WeightValue(projectid);
                    //直接赋权是判断准则评权是否完成
                    float allrulevalue = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[1].ToString().Equals("准则"))
                        {
                            allrulevalue += Convert.ToSingle(dr[2].ToString());
                            //直接赋权下判断各指标评权是否完成
                            float allindivalue = 0;
                            foreach (DataRow dr1 in dt.Rows)
                            {
                                if (dr1[1].ToString().Equals("指标"))
                                {
                                    //Convert.ToInt32(dr1[0].ToString()) 是指标ID
                                    if (MasApp.BLL.Search.RuleID(Convert.ToInt32(dr1[0].ToString())) == int.Parse(dr[0].ToString()))
                                    {
                                        allindivalue += Convert.ToSingle(dr1[2].ToString());
                                    }
                                }
                            }
                            if ((allindivalue - 1F) > 0.0000001F)
                            {
                                Label lb = new Label();
                                lb.Text = "尚有项目任务未完成2，不能生成结果！";
                                lb.ForeColor = System.Drawing.Color.Red;
                                lb.Font.Size = FontUnit.Larger;
                                form1.Controls.AddAt(1, lb);
                                HyperLink hy = new HyperLink();
                                hy.NavigateUrl = "list.aspx";
                                hy.Text = "<br />返回";
                                form1.Controls.Add(hy);
                            }
                        }
                    }
                    if ((allrulevalue - 1F) > 0.0000001F)
                    {
                        Label lb = new Label();
                        lb.Text = "尚有项目任务未完成3，不能生成结果！";
                        lb.ForeColor = System.Drawing.Color.Red;
                        lb.Font.Size = FontUnit.Larger;
                        form1.Controls.AddAt(1, lb);
                        HyperLink hy = new HyperLink();
                        hy.NavigateUrl = "list.aspx";
                        hy.Text = "<br />返回";
                        form1.Controls.Add(hy);
                    }
                }
                //app.BLL.response.alert("所有项目任务已完成，正在生成结果，请点击。。");
                write();
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法链接！", "list.aspx");
            }

        }
    }

    protected void write()
    {
        #region 图
        //定义X坐标Y坐标
        string strCategory = "";// 数据之间加 '\t'        
        string strValue = "";//Y轴数据 ，数据之间加 '\t'
        //定义X坐标Y坐标2
        string strCategory2 = "";// 数据之间加 '\t'        
        string strValue2 = "";//Y轴数据 ，数据之间加 '\t'
        #endregion

        Label lb = new Label();
        lb.Text = "<br />综合模糊评价法结果：";
        lb.ID = "target";
        form1.Controls.Add(lb);
        int projectid = int.Parse(Request.QueryString[0].ToString());
        //dt_weightvalue(elementid, elementtype, weightvalue)
        DataTable dt_weightvalue = MasApp.BLL.Search.WeightValue(projectid);

        //开始rule(ruleid, rulename)
        DataTable dt_rule = MasApp.BLL.Search.SearchRuleIDandName(projectid);
        //levelmission( levelmissionid )
        DataTable dt_levelmissionid = MasApp.BLL.Search.LevelMissionIDs(projectid);
        //用来保存目标值
        float value4 = 0;
        //这个是遍历准则
        foreach (DataRow dr1 in dt_rule.Rows)
        {
            lb = new Label();

            lb.Text = "<br />【准则】;" + dr1[1].ToString();
            lb.ID = "rule" + dr1[0].ToString();
            form1.Controls.Add(lb);
            strCategory += "\t" + dr1[1].ToString();
            strCategory2 += "\t" + dr1[1].ToString();
            //准则级值
            float value3 = 0F;
            //indicator( indicatorid, indicatorname)
            DataTable dt_indi = MasApp.BLL.Search.SearchIndicatorIDsandNames(Convert.ToInt32(dr1[0].ToString()));
            //遍历指标
            foreach (DataRow dr2 in dt_indi.Rows)
            {
                lb = new Label();
                lb.Text = "&nbsp;&nbsp;&nbsp;&nbsp;【指标】：" + dr2[1].ToString();
                form1.Controls.Add(lb);
                float value1 = 0;
                //遍历评级任务ID
                foreach (DataRow dr3 in dt_levelmissionid.Rows)
                {
                    float levelvalue = MasApp.DAL.Search.LevelValue(Convert.ToInt32(dr3[0].ToString()), Convert.ToInt32(dr2[0].ToString()));
                    value1 += levelvalue;
                }
                //求指标平均级值
                value1 = value1 / Convert.ToSingle(dt_levelmissionid.Rows.Count);
                lb = new Label();
                lb.Text = "&nbsp;&nbsp;&nbsp;&nbsp;[平均极值：" + value1.ToString("F4") + "&nbsp;]";
                form1.Controls.Add(lb);
                //求指标的权值,假设是  序关系法 
                DataTable weightmissionids = MasApp.BLL.Search.SearchWeightMissionIDS(projectid);
                int elementid = int.Parse(dr2[0].ToString());
                //查找当前指标的平均权值
                MasApp.Model.weight we = new MasApp.Model.weight();
                we.Elementid = elementid;
                float value2 = 0.0F;
                for (int wei = 0; wei < weightmissionids.Rows.Count; wei++)
                {
                    we.Weightmissionid = Convert.ToInt32(weightmissionids.Rows[wei][0].ToString());
                    we.Elementtype = "指标";
                    //得到权值
                    value2 += MasApp.BLL.Search.SearhWeightValue(we);
                }
                value2 = value2 / weightmissionids.Rows.Count;
                lb = new Label();
                lb.Text = "；&nbsp;&nbsp;&nbsp;&nbsp;[权值：" + value2.ToString("F4") + "]" + "<br />";
                form1.Controls.Add(lb);
                //得出准则级值
                value3 += value1 * value2;
            }
            //显示极值
            ((Label)form1.FindControl("rule" + dr1[0].ToString())).Text += "&nbsp;&nbsp;&nbsp;&nbsp;[平均极值：" + value3.ToString("F4") + "&nbsp;]";
            strValue += "\t" + value3.ToString();
            //求当前准则的权值,假设是直接赋权的
            int weightmissionid2 = MasApp.BLL.Search.SearchWeightMissionID(projectid);
            int elementid2 = int.Parse(dr1[0].ToString());
            //查找当前准则的权值
            MasApp.Model.weight we2 = new MasApp.Model.weight();
            we2.Elementid = elementid2;
            we2.Weightmissionid = weightmissionid2;
            we2.Elementtype = "准则";
            //得到权值
            float value5 = MasApp.BLL.Search.SearhWeightValue(we2);
            //显示
            ((Label)form1.FindControl("rule" + dr1[0].ToString())).Text += "；&nbsp;&nbsp;&nbsp;&nbsp;[权值：" + value5.ToString("F4") + "&nbsp;]" + "<br />";
            strValue2 += "\t" + value5.ToString();
            //目标值++
            value4 += value5 * value3;
        }
        //value4 即是目标值
        //取得 level(levelid, levelname, judgement, levelvalue) 序列按levelvalue降序
        DataTable dt_levelvalue2 = MasApp.DAL.Search.Levelvalue2(projectid);
        //判断等级
        int levelid = 0;
        string levelname = "";
        //遍历等级值
        foreach (DataRow dr4 in dt_levelvalue2.Rows)
        {
            if (value4 >= Convert.ToSingle(dr4[3].ToString()))
            {
                levelid = Convert.ToInt32(dr4[0].ToString());
                levelname = dr4[1].ToString();
                break;
            }
            else
            {
                levelid = Convert.ToInt32(dr4[0].ToString());
                levelname = dr4[1].ToString();
            }
        }
        lb = new Label();
        lb.Text = "<br />当前项目" + projectid.ToString() + "的评价对象的【目标】等级为：<font color=red>" + levelname + "&nbsp;&nbsp;&nbsp;[目标值为：" + value4.ToString("F4") + "]</font>。";
        form1.Controls.Add(lb);
        Image img1 = new Image();
        string filename = projectid + "rulelevel.gif";
        if (File.Exists(Server.MapPath(".") + @"\images\" + filename))
        {
            File.Delete(Server.MapPath(".") + @"\images\" + filename);
        }
        Image img2 = new Image();
        string filename2 = projectid + "ruleweight.gif";
        if (File.Exists(Server.MapPath(".") + @"\images\" + filename2))
        {
            File.Delete(Server.MapPath(".") + @"\images\" + filename2);
        }

        //#region 图2
        //声明对象
        //ChartSpace ThisChart = new ChartSpaceClass();
        //ChChart ThisChChart = ThisChart.Charts.Add(0);
        //ChSeries ThisChSeries = ThisChChart.SeriesCollection.Add(0);
        ////声明对象2
        //ChartSpace ThisChart2 = new ChartSpaceClass();
        //ChChart ThisChChart2 = ThisChart2.Charts.Add(0);
        //ChSeries ThisChSeries2 = ThisChChart2.SeriesCollection.Add(0);

        ////显示图例
        //ThisChChart.HasLegend = true;
        ////生成图片的标题
        //ThisChChart.HasTitle = true;
        //ThisChChart.Title.Caption = "模糊综合评价法结果--准则等级值";
        ////显示图例
        //ThisChChart2.HasLegend = true;
        ////生成图片的标题
        //ThisChChart2.HasTitle = true;
        //ThisChChart2.Title.Caption = "模糊综合评价法结果--准则权重值";

        ////图表类型
        //ThisChChart2.Type = ChartChartTypeEnum.chChartTypeRadarLineMarkers;
        //ThisChChart.Type = ChartChartTypeEnum.chChartTypeRadarLineMarkers;

        //////给定series的名字
        //ThisChSeries.SetData(ChartDimensionsEnum.chDimSeriesNames,
        //   ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), "极值");
        ////给定分类
        //ThisChSeries.SetData(ChartDimensionsEnum.chDimCategories,
        //    ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory.Substring(1));
        ////给定值
        //ThisChSeries.SetData(ChartDimensionsEnum.chDimValues,
        //    ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue.Substring(1));
        ////导出图像文件
        //string path = Server.MapPath(".") + @"\images\" + filename;
        //ThisChart.ExportPicture(path, "gif", 700, 450);
        //img1.ImageUrl = @"images\" + filename;


        ////给定series的名字
        //ThisChSeries2.SetData(ChartDimensionsEnum.chDimSeriesNames,
        //    ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), "权值");
        ////给定分类
        //ThisChSeries2.SetData(ChartDimensionsEnum.chDimCategories,
        //    ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory2.Substring(1));
        ////给定值
        //ThisChSeries2.SetData(ChartDimensionsEnum.chDimValues,
        //    ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue2.Substring(1));
        ////导出图像文件
        //string path2 = Server.MapPath(".") + @"\images\" + filename2;
        //ThisChart2.ExportPicture(path2, "gif", 700, 450);
        //img2.ImageUrl = @"images\" + filename2;
        //#endregion 图2

        //form1.Controls.AddAt(1, img1);
        //form1.Controls.AddAt(2, img2);
    }
}
